/**
 * Created by ehp on 2017/12/4.
 */

//数据分组，4行一组
function makeData(resultJson){
    /**
     *  把json对象里面的data遍历出来，并分组存放
     *  @flag       记录现在进行的下标数
     *  @group      记录分组 4个flag就换一个分组
     *  @time       从8点开始，并加入到数组中
     *  @dataObj    最后整合的数据
     */
    var jsonObj = resultJson.data;
    var flag = 0;
    var group = -1;
    var dataObj = [];
    var time = 8;
    //用try的原因，因为没有forEach没有break
    try {
        //遍历
        jsonObj.data.forEach(function (item) {
            //超过15跳出循环
            if (flag > 15) {
                foreach.break = new Error('Stop');
            }
            //分组
            if (flag % 4 == 0) {
                group++;
                dataObj[group] = [];
            }
            //塞数据
            item.time = time++;
            var newItem = {
                qixi:{
                    use:item.quse,
                    total:item.qtotal
                },
                course:{
                    course:item.cuse,
                    total:item.ctotal
                }
            };
            dataObj[group].push(item);
            flag++;
        });
    } catch (e) {}
       //console.log('data.length over 16');
    return dataObj;
}
function putIn(){

}

/**
 * @storageQ 用来存选中的器械
 * @storageC 用来存选中的课程
 */


var storageQ = [];
var storageC = [];
var storageD = '';
var app = angular.module('myApp', []);
//默认
app.controller('myCtrl', function($scope, $rootScope, $http) {
    /**
     *  还要去重
     */
    var date = new Date().pattern('yyyy.MM.dd');
    //var url = "/bdJianShen/public/index.php/api/Venue/VenueDetails2?place_id=2&datetime="+date;
    var url = "./json.html";
    $http.get(url).then(function(result){
        var dataObj = makeData(result);
        $rootScope.data = dataObj;
        //选中逻辑
        $scope.selectQ = function(){
            //去重
            var index;
            index = $.inArray(this.item, storageQ);
            if(index != -1){
                this.qstyle = {
                    "color":"#ff941a",
                    "border-color":"#ff941a"
                }
                storageQ.splice(index , 1);
                return;
            }

            //如果没有重复
            console.log(this.item);
            var item = this.item;
            var objData = {
                time:   item.time,
                quse:   item.quse,
                qtotal: item.qtotal
            };
            console.log(objData);
            storageQ.push(this.item);
            this.qstyle = {
                "color":"#999999",
                "border-color":"#999999"
            }
        }
        $scope.selectC = function(){
            var index;
            index = $.inArray(this.item, storageC);
            if(index != -1){
                this.cstyle = {
                    "color":"#ff941a",
                    "border-color":"#ff941a"
                }
                storageC.splice(index , 1);
                return;
            }

            console.log(this.item);
            storageC.push(this.item);
            this.cstyle = {
                "color":"#999999",
                "border-color":"#999999"
            }
        }
    });
});

app.controller('dateCtrl', function($scope, $rootScope, $http){

    var dateArr = [];
    var timestamp = Date.parse(new Date());
    var inDate = new Date();

    //init
    dateArr[0] = {};
    dateArr[0].week = '今天';
    dateArr[0].day = inDate.pattern('MM.dd');
    dateArr[0].date = inDate.pattern('yyyy.MM.dd');
    dateArr[1] = {};
    dateArr[1].week = '明天';
    dateArr[1].day = inDate.pattern('MM.dd');
    dateArr[1].date = inDate.pattern('yyyy.MM.dd');
    storageD = dateArr[0].date;
    //forEach
    for(var i=2;i<7;i++){
        inDate.setTime(timestamp + i * 3600 * 24 *1000);
        dateArr[i] = {};
        dateArr[i].week = inDate.pattern('EE');
        dateArr[i].day = inDate.pattern('MM.dd');
        dateArr[i].date = inDate.pattern('yyyy.MM.dd');
    }
    $scope.dateArr = dateArr;
    $scope.today = '12.05';
    $scope.tomorrow = '12.06';

    setTimeout(()=>{
        makeSlide();
    },0);

    $scope.selectD = function(){
        storageD = this.x.date;
        var url = "/bdJianShen/public/index.php/api/Venue/VenueDetails2?place_id=2&datetime="+this.x.date;
        $http.get(url).then(function(result){
            var dataObj = makeData(result);
            $rootScope.data = dataObj;
        });
    };
});

window.onload = function(){

    $('#go').click(function(){

        var jsonQ = JSON.stringify(storageQ);
        var jsonC = JSON.stringify(storageC);

        localStorage.setItem('qixi',jsonQ);
        localStorage.setItem('course',jsonC);
        localStorage.setItem('date',storageD);

        window.location.href = './conf_appoint.html';
    });
}
/*try {
 // 如果现在操作的id（this.$id） 包含在 上一个id LastQ.$id 或者 LastC.$id
 /!*if(this.$id != lastQ.$id && this.$id != lastC.$id){
 lastQ.qstyle = {"color":"#ff8903"}
 lastC.cstyle = {"color":"#ff8903"}
 }*!/
 } catch (e) {}*/
//其他模块，要click


